package com.mashibing.internalcommon.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.mashibing.internalcommon.dto.TokenResult;

import java.time.Period;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.mashibing.internalcommon.constant.IdentityConstants.PASSENGER_IDENTITY;

public class JwtUtils {
    /**
     * 这段代码用于获取一个基于当前时间往后一天的具体时间并用date存储。
     */
    private static final String SIGN = "CPFmsb!@#$$";

    private static final String JWT_KEY_PHONE = "phone";

    // 乘客是1，司机是2
    private static final String JWT_KEY_IDENTITY = "identity";

    // token类型
    private static final String JWT_TOKEN_TYPE = "tokenType";

    private static final String JWT_TOKEN_TIME = "tokenTime";
    public static String generatorToken(String phone, String identity,String tokenType) {
        Map<String, String> map = new HashMap<>();
        map.put(JWT_KEY_PHONE,phone);
        map.put(JWT_KEY_IDENTITY, identity);
map.put(JWT_TOKEN_TYPE,tokenType);
map.put(JWT_TOKEN_TIME,Calendar.getInstance().getTime().toString());


        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);
        Date date = calendar.getTime();


        JWTCreator.Builder builder = JWT.create();
        // 整合map
        map.forEach(
                (k, v) -> {
                    builder.withClaim(k, v);
                }
        );
        //整合过期时间
//        builder.withExpiresAt(date);
        //sheng生成token字符串
        String sign = builder.sign(Algorithm.HMAC256(SIGN));
        return sign;
    }

    public static TokenResult parseToken(String token) {

        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
        String phone = verify.getClaim(JWT_KEY_PHONE).asString();
        String identity = verify.getClaim(JWT_KEY_IDENTITY).asString();
        TokenResult tokenResult = new TokenResult();
        tokenResult.setIdentity(identity);
        tokenResult.setPhone(phone);
        return tokenResult;
    }

    /**
     * 校验token，判断token是否异常
     * @param token
     * @return
     */
    public static TokenResult checkToken(String token){
        TokenResult tokenResult= null;
    try {
        tokenResult = JwtUtils.parseToken(token);
    } catch (Exception e){

    }
    return tokenResult;
}

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zhangsan");
        map.put("age", "3");
        String s = generatorToken("15830297678",PASSENGER_IDENTITY,"access");
        System.out.println(s);
        TokenResult tokenResult = parseToken(s);
      // System.out.println(tokenResult.getIdentity()+tokenResult.getPhone());
        System.out.println(tokenResult.toString());
    }
}

